// @algorithm @lc id=538 lang=cpp 
// @title convert-bst-to-greater-tree


#define print(...)
// @test([4,1,6,0,2,5,7,null,null,null,3,null,null,null,8])=[30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]
// @test([0,null,1])=[1,null,1]
// @test([1,0,2])=[3,3,2]
// @test([3,2,4,1])=[7,9,4,10]
class Solution {
public:
    TreeNode* convertBST(TreeNode* root) {
        function<int(TreeNode*, int)> convert = [&](TreeNode *node, int sum){
            if(node==NULL) return sum;
            print("val=", node->val, ", sum=", sum, "\n")
            node->val += convert(node->right, sum);
            print("newVal=", node->val, "\n")
            return convert(node->left, node->val);
        };
        convert(root, 0);
        return root;
    }
};
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */